package org.catdroid.app.opina;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends Activity {
	
	// using SoundPool scheme
	SoundPool soundPool;
	Integer soundYay;
	Integer soundBoo;
	AudioManager mgr;
	int streamVolume = 0;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        // set up listener for logo button
        ImageButton btnLogo = (ImageButton) findViewById(R.id.btnLogo);
        btnLogo.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Launch About activity
            	Intent i=new Intent(getBaseContext(), AboutActivity.class);
        		startActivity(i);
            }
        });
        // init soundPool
        soundPool = new SoundPool(1,AudioManager.STREAM_MUSIC,0);
        setVolumeControlStream(AudioManager.STREAM_MUSIC); // physical volume buttons will apply to media volume, not ringer
        // get current volume level
    	mgr = (AudioManager) this.getSystemService(MainActivity.AUDIO_SERVICE);
        streamVolume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
        // load sounds from R
        soundYay = soundPool.load(this, R.raw.crowdc, 0);
        soundBoo = soundPool.load(this, R.raw.boos2, 0);
    }
    
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
	/** Called when user clicks on "Menu" button. */
    	// create a menu from the definition in menu/menu.xml
    	super.onCreateOptionsMenu(menu);
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu, menu);
    	return true;
    }
	
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    /** Called when user chooses an item from the menu. */
    	// Act differently depending on the menu item clicked
    	switch (item.getItemId()) {
    	case R.id.about_menu:
    		Intent i=new Intent(this,AboutActivity.class);
    		startActivity(i);
    		return true;
    	}
    	return false;
    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
    	// force screen to always be vertical
        super.onConfigurationChanged(newConfig);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    
    public void playSound(View view) {
    	/** Function called by button onClick as defined in main.xml. */
    	
        int maxStreamVolume = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC); // get max volume; we will use this max volume for our sounds
        if (!(mgr.isMusicActive())) { // a sound is not playing yet
        	mgr.setStreamVolume(AudioManager.STREAM_MUSIC, maxStreamVolume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); // set current media volume to max without showing any clues to user
        	switch (view.getId()) {
        	case R.id.btnYes:
        		// play yay! sound
        		soundPool.play(soundYay, 1, 1, 0, 0, 1);
        		vibrate();
        		break;
        	case R.id.btnNo:
        		// play boo! sound
        		soundPool.play(soundBoo, 1, 1, 0, 0, 1);
        		vibrate();
        		break;
        	}
        }
    }
    
    public void vibrate(){
    	// Vibrate for 100 miliseconds
    	Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
    	vibrator.vibrate(100);
    }
    
    @Override
    protected void onDestroy() {
    	/** Called when closing app. */
    	// release audio resources
        soundPool.release();
        // return sound level to initial
    	mgr.setStreamVolume(AudioManager.STREAM_MUSIC, streamVolume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);       
        // unlock physical volume buttons to media volume
        setVolumeControlStream(AudioManager.USE_DEFAULT_STREAM_TYPE);    	
        super.onDestroy();
    }
}